//
//
//package com.geek.bams.server;
//
//import com.geek.bams.service.Bank;
//import com.geek.bams.vo.RankVO;
//
//import java.awt.*;
//import java.awt.event.*;
//import java.util.TreeSet;
//import javax.swing.*;
//import javax.swing.table.DefaultTableModel;
//
//
//public class RankPanel extends JPanel {
//
//    Bank bank = Bank.getInstance();
//
//    public RankPanel() {
//        initComponents();
//    }
//
//    //点击按钮查看总余额
//    private void totalBalance(ActionEvent e) {
//        double balance = bank.totalBalance();
//        moneyLabel.setText(balance+"元");
//        scrollPane.setViewportView(moneyLabel);
//    }
//
//    //点击按钮查看总透支额度
//    private void totalCeiling(ActionEvent e) {
//        double ceiling = bank.totalCeiling();
//        moneyLabel.setText(ceiling+"元");
//        scrollPane.setViewportView(moneyLabel);
//    }
//
//    //点击按钮查看总贷款额度
//    private void totalLoanAmount(ActionEvent e) {
//        double loanAmount = bank.totalLoanAmount();
//        moneyLabel.setText(loanAmount+"元");
//        scrollPane.setViewportView(moneyLabel);
//    }
//
//    //点击按钮查看排行榜
//    private void rank(ActionEvent e) {
//        TreeSet<RankVO> rank = bank.rank();
//
//        //表格标题
//        String[] titles = {"序号" , "身份证号" , "姓名" , "总资产"};
//
//        //表格数据
//        Object[][] data = new Object[rank.size()][titles.length];
//        //序号
//        int index = 0;
//        for (RankVO rankVO : rank) {
//            data[index] = new Object[]{
//                    ++index,
//                    rankVO.getPersonID(),
//                    rankVO.getName(),
//                    rankVO.getTotal()
//            };
//        }
//
//        //表格中填充数据
//        DefaultTableModel tableModel = new DefaultTableModel(data , titles);
//        JTable table = new JTable(tableModel);
//        //将表格添加至scrollPane中
//        scrollPane.setViewportView(table);
//    }
//
//    //点击返回，可以跳转至服务界面
//    private void back(ActionEvent e) {
//        CardLayout card = (CardLayout)this.getParent().getLayout();
//        card.show(this.getParent() , "serverPanel");
//
//        //清空数据
//        moneyLabel.setText("0.0元");
//        moneyLabel.setHorizontalAlignment(SwingConstants.CENTER);
//        scrollPane.setViewportView(moneyLabel);
//    }
//
//    private void initComponents() {
//        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
//        totalBalanceButton = new JButton();
//        totalCeilingButton = new JButton();
//        totalLoanAmountButton = new JButton();
//        rankButton = new JButton();
//        backButton = new JButton();
//        scrollPane = new JScrollPane();
//        moneyLabel = new JLabel();
//
//        //======== this ========
//        setLayout(null);
//
//        //---- totalBalanceButton ----
//        totalBalanceButton.setText("\u67e5\u770b\u603b\u4f59\u989d");
//        totalBalanceButton.addActionListener(e -> totalBalance(e));
//        add(totalBalanceButton);
//        totalBalanceButton.setBounds(35, 70, 115, totalBalanceButton.getPreferredSize().height);
//
//        //---- totalCeilingButton ----
//        totalCeilingButton.setText("\u67e5\u770b\u900f\u652f\u603b\u989d");
//        totalCeilingButton.addActionListener(e -> totalCeiling(e));
//        add(totalCeilingButton);
//        totalCeilingButton.setBounds(35, 170, 115, 34);
//
//        //---- totalLoanAmountButton ----
//        totalLoanAmountButton.setText("\u67e5\u770b\u8d37\u6b3e\u603b\u989d");
//        totalLoanAmountButton.addActionListener(e -> totalLoanAmount(e));
//        add(totalLoanAmountButton);
//        totalLoanAmountButton.setBounds(35, 270, 115, 34);
//
//        //---- rankButton ----
//        rankButton.setText("\u67e5\u770b\u6392\u884c\u699c");
//        rankButton.addActionListener(e -> rank(e));
//        add(rankButton);
//        rankButton.setBounds(35, 370, 115, 34);
//
//        //---- backButton ----
//        backButton.setText("\u8fd4\u56de");
//        backButton.addActionListener(e -> back(e));
//        add(backButton);
//        backButton.setBounds(35, 470, 115, 34);
//
//        //======== scrollPane ========
//        {
//
//            //---- moneyLabel ----
//            moneyLabel.setText("0.0\u5143");
//            moneyLabel.setHorizontalAlignment(SwingConstants.CENTER);
//            scrollPane.setViewportView(moneyLabel);
//        }
//        add(scrollPane);
//        scrollPane.setBounds(170, 70, 315, 430);
//
//        setPreferredSize(new Dimension(500, 650));
//        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
//    }
//
//    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
//    private JButton totalBalanceButton;
//    private JButton totalCeilingButton;
//    private JButton totalLoanAmountButton;
//    private JButton rankButton;
//    private JButton backButton;
//    private JScrollPane scrollPane;
//    private JLabel moneyLabel;
//    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
//}



package com.geek.bams.server;

import com.geek.bams.service.Bank;
import com.geek.bams.vo.RankVO;

import java.awt.*;
import java.awt.event.*;
import java.util.TreeSet;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class RankPanel extends JPanel {

    Bank bank = Bank.getInstance();
    private ScheduledExecutorService scheduler;
    private boolean isAutoRefresh = false;
    public RankPanel() {
        initComponents();
        initAutoRefresh();
    }

    private void initAutoRefresh() {
        scheduler = Executors.newScheduledThreadPool(1);
    }

    private void startAutoRefresh() {
        if (!isAutoRefresh) {
            isAutoRefresh = true;
            scheduler.scheduleAtFixedRate(this::refreshRankData, 0, 5, TimeUnit.SECONDS);
        }
    }

    private void stopAutoRefresh() {
        if (isAutoRefresh) {
            isAutoRefresh = false;
        }
    }

    private void refreshRankData() {
        SwingUtilities.invokeLater(() -> {
            // 只有在排行榜面板可见时才刷新
            if (this.isVisible() && isAutoRefresh) {
                rank(null);
            }
        });
    }

    //点击按钮查看排行榜
    private void rank(ActionEvent e) {
        TreeSet<RankVO> rank = bank.rank();

        //表格标题
        String[] titles = {"序号" , "身份证号" , "姓名" , "总资产"};

        //表格数据
        Object[][] data = new Object[rank.size()][titles.length];
        //序号
        int index = 0;
        for (RankVO rankVO : rank) {
            data[index] = new Object[]{
                    ++index,
                    rankVO.getPersonID(),
                    rankVO.getName(),
                    rankVO.getTotal()
            };
        }

        //表格中填充数据
        DefaultTableModel tableModel = new DefaultTableModel(data , titles);
        JTable table = new JTable(tableModel);
        //将表格添加至scrollPane中
        scrollPane.setViewportView(table);
    }

    // 重写 setVisible 方法，控制自动刷新
    @Override
    public void setVisible(boolean aFlag) {
        super.setVisible(aFlag);
        if (aFlag) {
            startAutoRefresh();
        } else {
            stopAutoRefresh();
        }
    }

    // ... existing code ...

    //点击按钮查看总余额
    private void totalBalance(ActionEvent e) {
        double balance = bank.totalBalance();
        moneyLabel.setText(balance+"元");
        scrollPane.setViewportView(moneyLabel);
    }

    //点击按钮查看总透支额度
    private void totalCeiling(ActionEvent e) {
        double ceiling = bank.totalCeiling();
        moneyLabel.setText(ceiling+"元");
        scrollPane.setViewportView(moneyLabel);
    }

    //点击按钮查看总贷款额度
    private void totalLoanAmount(ActionEvent e) {
        double loanAmount = bank.totalLoanAmount();
        moneyLabel.setText(loanAmount+"元");
        scrollPane.setViewportView(moneyLabel);
    }



    //点击返回，可以跳转至服务界面
    private void back(ActionEvent e) {
        CardLayout card = (CardLayout)this.getParent().getLayout();
        card.show(this.getParent() , "serverPanel");

        //清空数据
        moneyLabel.setText("0.0元");
        moneyLabel.setHorizontalAlignment(SwingConstants.CENTER);
        scrollPane.setViewportView(moneyLabel);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        totalBalanceButton = new JButton();
        totalCeilingButton = new JButton();
        totalLoanAmountButton = new JButton();
        rankButton = new JButton();
        backButton = new JButton();
        scrollPane = new JScrollPane();
        moneyLabel = new JLabel();

        //======== this ========
        setLayout(null);

        //---- totalBalanceButton ----
        totalBalanceButton.setText("\u67e5\u770b\u603b\u4f59\u989d");
        totalBalanceButton.addActionListener(e -> totalBalance(e));
        add(totalBalanceButton);
        totalBalanceButton.setBounds(35, 70, 115, totalBalanceButton.getPreferredSize().height);

        //---- totalCeilingButton ----
        totalCeilingButton.setText("\u67e5\u770b\u900f\u652f\u603b\u989d");
        totalCeilingButton.addActionListener(e -> totalCeiling(e));
        add(totalCeilingButton);
        totalCeilingButton.setBounds(35, 170, 115, 34);

        //---- totalLoanAmountButton ----
        totalLoanAmountButton.setText("\u67e5\u770b\u8d37\u6b3e\u603b\u989d");
        totalLoanAmountButton.addActionListener(e -> totalLoanAmount(e));
        add(totalLoanAmountButton);
        totalLoanAmountButton.setBounds(35, 270, 115, 34);

        //---- rankButton ----
        rankButton.setText("\u67e5\u770b\u6392\u884c\u699c");
        rankButton.addActionListener(e -> rank(e));
        add(rankButton);
        rankButton.setBounds(35, 370, 115, 34);

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.addActionListener(e -> back(e));
        add(backButton);
        backButton.setBounds(35, 470, 115, 34);

        //======== scrollPane ========
        {

            //---- moneyLabel ----
            moneyLabel.setText("0.0\u5143");
            moneyLabel.setHorizontalAlignment(SwingConstants.CENTER);
            scrollPane.setViewportView(moneyLabel);
        }
        add(scrollPane);
        scrollPane.setBounds(170, 70, 315, 430);

        setPreferredSize(new Dimension(500, 650));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton totalBalanceButton;
    private JButton totalCeilingButton;
    private JButton totalLoanAmountButton;
    private JButton rankButton;
    private JButton backButton;
    private JScrollPane scrollPane;
    private JLabel moneyLabel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}

